package net.awpspace.matheasy;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Button {
	public Image up, down;
	public boolean isPressed;
	public int x, y;
	private ButtonListener listener;

	public Button(Image up, Image down, int x, int y) {
		this.up = up;
		this.down = down;
		this.x = x;
		this.y = y;
	}

	public void setListener(ButtonListener listener) {
		this.listener = listener;
	}

	public void actionUp(int x, int y) {

		if (isPressed && contain(x, y) && listener != null)
			listener.actionUp();
		isPressed = false;
	}

	public void actionDown(int x, int y) {		
		if (contain(x, y)) {
			System.out.println(x + "  " + y);
			isPressed = true;
			if (listener != null)
				listener.actionDown();

		}
	}

	public boolean contain(int x, int y) {
		return this.x < x && x < this.x + up.getWidth() && this.y < y
				&& y < this.y + up.getHeight();
	}

	public void draw(Graphics g) {
		if (isPressed)
			g.drawImage(down, x, y, Graphics.TOP | Graphics.LEFT);
		else
			g.drawImage(up, x, y, Graphics.TOP | Graphics.LEFT);
	}

}
